{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "#reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")\n",
    "#导入数据\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sbn\n",
    "#实时绘图\n",
    "%matplotlib inline "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 对连续型特征，可以用哪个函数可视化其分布？（给出你最常用的一个即可），并根据代码运行结果给出示例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>RAD</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>B</th>\n",
       "      <th>LSTAT</th>\n",
       "      <th>MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.00632</td>\n",
       "      <td>18</td>\n",
       "      <td>2.31</td>\n",
       "      <td>0</td>\n",
       "      <td>0.538</td>\n",
       "      <td>6.575</td>\n",
       "      <td>65.2</td>\n",
       "      <td>4.0900</td>\n",
       "      <td>1</td>\n",
       "      <td>296</td>\n",
       "      <td>15</td>\n",
       "      <td>396.90</td>\n",
       "      <td>4.98</td>\n",
       "      <td>24.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.02731</td>\n",
       "      <td>0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>6.421</td>\n",
       "      <td>78.9</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2</td>\n",
       "      <td>242</td>\n",
       "      <td>17</td>\n",
       "      <td>396.90</td>\n",
       "      <td>9.14</td>\n",
       "      <td>21.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.02729</td>\n",
       "      <td>0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>7.185</td>\n",
       "      <td>61.1</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2</td>\n",
       "      <td>242</td>\n",
       "      <td>17</td>\n",
       "      <td>392.83</td>\n",
       "      <td>4.03</td>\n",
       "      <td>34.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.03237</td>\n",
       "      <td>0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>6.998</td>\n",
       "      <td>45.8</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3</td>\n",
       "      <td>222</td>\n",
       "      <td>18</td>\n",
       "      <td>394.63</td>\n",
       "      <td>2.94</td>\n",
       "      <td>33.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.06905</td>\n",
       "      <td>0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>7.147</td>\n",
       "      <td>54.2</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3</td>\n",
       "      <td>222</td>\n",
       "      <td>18</td>\n",
       "      <td>396.90</td>\n",
       "      <td>5.33</td>\n",
       "      <td>36.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      CRIM  ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  PTRATIO  \\\n",
       "0  0.00632  18   2.31     0  0.538  6.575  65.2  4.0900    1  296       15   \n",
       "1  0.02731   0   7.07     0  0.469  6.421  78.9  4.9671    2  242       17   \n",
       "2  0.02729   0   7.07     0  0.469  7.185  61.1  4.9671    2  242       17   \n",
       "3  0.03237   0   2.18     0  0.458  6.998  45.8  6.0622    3  222       18   \n",
       "4  0.06905   0   2.18     0  0.458  7.147  54.2  6.0622    3  222       18   \n",
       "\n",
       "        B  LSTAT  MEDV  \n",
       "0  396.90   4.98  24.0  \n",
       "1  396.90   9.14  21.6  \n",
       "2  392.83   4.03  34.7  \n",
       "3  394.63   2.94  33.4  \n",
       "4  396.90   5.33  36.2  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#加载数据\n",
    "df = pd.read_csv(\"boston_housing.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000001693430CB70>,\n",
       "        <matplotlib.axes._subplots.AxesSubplot object at 0x000001693436ABA8>]],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEICAYAAABRSj9aAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uID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xgBruBEbJpiA9BNwAfBHYBvwM8AxwSUTUf2DbrXr+PfANYA9v9A9+mqw/tOc1SXoP2QdPc8hODrZFxGckvY3sbGc+8C3gIxHxarfrsWKU7bjsFUmjwKci4sJ+PeYLD3ozM+uuortuzMysyxz0ZmaJc9CbmSXOQW9mljgHvZlZ4hz0ZmaJc9CbmSXu/wO0j7eiQUIVewAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#以图形的方式显示\n",
    "#使用DataFrame的hist()方法也可以绘制直方图（histogram）\n",
    "features = [\"MEDV\", \"LSTAT\"]  #可以一次指定多个特征\n",
    "df[features].hist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上图可以看出，LSTAT低收入人群比例在10的附近比较集中，也就是随着收入增加人数越少，符合实际情况。MEDV中值的图也基本符合正态分布。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 对两个连续型特征，可以用哪个函数得到这两个特征之间的相关性？根据代码运行结果，给出示例。（10分） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x169344b0e10>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWQAAAD8CAYAAABAWd66AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAGNBJREFUeJzt3X+UXWV97/H3J4HwM2AqXDA/IEFBQfAmohEXwkIpkAKSIL1J4BICy95A+aUoUVQWBFBhFdCCpfTG2wi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t6Zls/YZG/ow2uPymXv9sBg6XtFtEbKH4Vdr2HufcExEXl+6QNBZ4KSImZNsHAT+WNCQifiBpA3AM8M/Z8Q8BwyPiF4P609hguhX4bkQ8ACDpiIh4huIcu2Rfkrg8Itq6L8iS8OkUv+d2LLA0In4A/CA7/jLw6Yh4vYE/hzWAW8j99xPglGy9X1+bjYh1wJeAS7Ndd1P8KkG36f25ryXlfRQ/BQRAloyr+TTwLHA7xT9b9i7hhNx/84HpknYFPgL8W4/j03qULHbr5T6/BD6UrS8Apkjq/s1lWvYca13fpfiBy59IukzSe2q4pvsf+H8CTpW086BGaMlwQu6niFgFjKX4l2dRmVPuiYjxJcuWXm617YsFEfEasBo4XtJ4YGtEPFvn0K2BslLDocCPgOOAFd3fWisn+3bbycD9EfF7iv/Qn9iAUC0BriEPzELgJop/0d7bz3tMAJ4r2e4uW/walytyIfv68DxgnqRngcOBJ3o5fRKwN/CMJIDdgbeAhxsQqjWZE/LAzAN+FxHPSDqurxdnnXw3Ad8r2X0f8G2Kfwk/M/AQrZkkTQJ+FhFbJe1P8R/unh3Apc4E/iIi7s6u3wNYL2n3iHhr8CO2ZnJCHoCIKAC39HJ4mqRPlWxfCGwE3i/pSWBX4A/A97Jfa7vv+aakFcB+EbF+kEK3wbG7pELJ9neA0cAtkv4r2zc7K03tQNLuwEnA+d37ImKzpJ8DnwXuGZywLRV+ddrMLBHu1DMzS4QTsplZIpyQzcwS4YRsZpYIJ2Qzs0Q4IZuZJcIJ2cwsEf8NuT/2v43cnIwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#使用DataFrame的corr()方法先计算出每对特征间的相关矩阵，然后将所得的相关矩阵传给seaborn的heatmap()方法，渲染出一个基于色彩编码的矩阵。\n",
    "fe_data = df[['MEDV','LSTAT']]\n",
    "data_corr = fe_data.corr()\n",
    "#只考虑正数情况\n",
    "data_corr = data_corr.abs()\n",
    "sbn.heatmap(data_corr,annot=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上图可以看出 LSTAT 和 MEDV 的相关系数为0.74 的特征属于强相关，（通常认为相关系数的绝对值大于0.5的特征为强相关）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 如果发现特征之间有较强的相关性，在选择线性回归模型时应该采取什么措施。（10分） \n",
    "   答：在数据探索阶段发现，发现各特征差异较大，需要进行数据标准化预处理。 标准化的目的在于避免原始特征值差异过大，导致训练得到的参数权重单位不一致，无法比较各特征的重要性。 另外，一些优化算法（如随机梯度下降及其改进版本）只在各特征尺度差不多的情况下才能保证收敛。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 当采用带正则的模型以及采用随机梯度下降优化算法时，需要对输入（连续型）特征进行去量纲预处理。课程代码给出了用标准化（StandardScaler）的结果，请改成最小最大缩放（MinMaxScaler）去量纲 （10分），并重新训练最小二乘线性回归、岭回归、和Lasso模型（30分）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LinearRegression coef: [[-0.31314149  0.0465753  -0.0146663   0.05281518 -0.13752482  0.25798151\n",
      "  -0.0014678  -0.24832043  0.00072879 -0.13482652  0.06684817 -0.45467051]]\n",
      "LinearRegression intercept: [0.74724998]\n",
      "Ridge coef: [[-0.2389979   0.0365123  -0.01501513  0.05476771 -0.11442898  0.25425655\n",
      "  -0.00569783 -0.2006679  -0.01632525 -0.13361463  0.07197209 -0.42842567]]\n",
      "Ridge intercept: [0.72452642]\n",
      "Lasso coef: [-0.          0.         -0.          0.         -0.          0.\n",
      " -0.          0.         -0.0737421  -0.          0.         -0.28549008]\n",
      "Lasso intercept: [0.72168716]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\preprocessing\\data.py:334: DataConversionWarning: Data with input dtype int64, float64 were all converted to float64 by MinMaxScaler.\n",
      "  return self.partial_fit(X, y)\n"
     ]
    }
   ],
   "source": [
    "# 从原始数据中分离输入特征x和输出y\n",
    "y = df['MEDV']\n",
    "X = df.drop('MEDV', axis = 1)\n",
    "\n",
    "# 尝试对y（房屋价格）做log变换，对log变换后的价格进行估计\n",
    "log_y = np.log1p(y)\n",
    "X[\"RAD\"].astype(\"object\")\n",
    "X_cat = X[\"RAD\"]\n",
    "X_cat = pd.get_dummies(X_cat, prefix=\"RAD\")\n",
    "\n",
    "X = X.drop(\"RAD\", axis = 1)\n",
    "# 数据标准化\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "#导入训练模型\n",
    "from sklearn.linear_model import  LinearRegression, Ridge ,Lasso\n",
    "# 分别初始化对特征和目标值的标准化器\n",
    "ss_X = MinMaxScaler()\n",
    "ss_y = MinMaxScaler()\n",
    "ss_log_y = MinMaxScaler()\n",
    "\n",
    "# 分别对训练和测试数据的特征以及目标值进行标准化处理\n",
    "# 对训练数据，先调用fit方法训练模型，得到模型参数；然后对训练数据和测试数据进行transform\n",
    "X = ss_X.fit_transform(X)\n",
    "#对y做标准化不是必须\n",
    "#对y标准化的好处是不同问题的w差异不太大，同时正则参数的范围也有限\n",
    "y = ss_y.fit_transform(y.values.reshape(-1, 1))\n",
    "log_y = ss_y.fit_transform(log_y.values.reshape(-1, 1))\n",
    "\n",
    "lrg = LinearRegression()\n",
    "ridge = Ridge()\n",
    "lasso = Lasso(alpha=0.015)\n",
    "lrg.fit(X,log_y)\n",
    "ridge.fit(X,log_y)\n",
    "lasso.fit(X,log_y)\n",
    "print('LinearRegression coef: {}'.format(lrg.coef_[:]))\n",
    "print(\"LinearRegression intercept: {}\".format(lrg.intercept_))\n",
    "print('Ridge coef: {}'.format(ridge.coef_[:]))\n",
    "print(\"Ridge intercept: {}\".format(ridge.intercept_))\n",
    "print('Lasso coef: {}'.format(lasso.coef_[:]))\n",
    "print(\"Lasso intercept: {}\".format(lasso.intercept_))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们注意到Lasso训练后的的系数部分为0，跟alpha的取值有关，经过调整alpha参数发现，随着alpha的增加大coef系数趋近于0，这里使用线性回归和岭回归就可以了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 代码中给出了岭回归（RidgeCV）和Lasso（LassoCV）超参数（alpha_）调优的过程，请结合两个最佳模型以及最小二乘线性回归模型的结果，给出什么场合应该用岭回归，什么场合用Lasso，什么场合用最小二乘。（30分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答：交叉验证可以评价一个超参数取值𝜆对应模型的性能e𝜆,如果需要超参数调优通过遍历不同的超参数取值，选择性能最好的超参数𝜆∗ ，在给定最优超参数𝜆∗情况下，用全体训练数据再次训练 模型，得到模型参数𝐰，得到最终模型𝑓(𝐱;𝐰,𝜆∗). 模型超参数调优。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RidgeCV coef_:  [[-0.2389979   0.0365123  -0.01501513  0.05476771 -0.11442898  0.25425655\n",
      "  -0.00569783 -0.2006679  -0.01632525 -0.13361463  0.07197209 -0.42842567]]\n",
      "RidgeCV intercept_: [0.72452642]\n",
      "RiegeCV alpha_: 1.0\n",
      "LassoCV coef_:  [-0.15891853  0.         -0.          0.04309191 -0.06031005  0.23238786\n",
      " -0.         -0.09051492 -0.02581078 -0.12978771  0.0616794  -0.46999172]\n",
      "LassoCV intercept_: 0.7057713081930328\n",
      "LassoCV alpha_: 0.001\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.\n",
      "  DeprecationWarning)\n",
      "c:\\users\\administrator\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\linear_model\\coordinate_descent.py:1109: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import RidgeCV,LassoCV\n",
    "rgcv = RidgeCV(alphas=[0.001,0.01,0.1, 1.0, 10.0],cv=3)\n",
    "lscv = LassoCV(alphas=[0.001,0.01,0.1, 1.0, 10.0],cv=3)\n",
    "rgcv.fit(X,log_y)\n",
    "lscv.fit(X,log_y)\n",
    "print('RidgeCV coef_: ',rgcv.coef_)\n",
    "print('RidgeCV intercept_:',rgcv.intercept_)\n",
    "print(\"RiegeCV alpha_:\",rgcv.alpha_)\n",
    "print('LassoCV coef_: ',lscv.coef_)\n",
    "print('LassoCV intercept_:',lscv.intercept_)\n",
    "print(\"LassoCV alpha_:\",lscv.alpha_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于直接套用线性回归可能产生过拟合，我们需要加入正则化项，如果加入的是L2正则化项，就是Ridge岭回归。它和一般线性回归的区别是在损失函数上增加了一个L2正则化的项，和一个调节线性回归项和正则化项权重的系数α。损失函数表达式如下：\n",
    "\n",
    "J(θ)=1/2(Xθ−Y)T(Xθ−Y)+1/2α||θ||22\n",
    "\n",
    "其中α为常数系数，需要进行调优。||θ||2为L2范数。Ridge回归的解法和一般线性回归大同小异。如果采用梯度下降法，则每一轮θ迭代的表达式是：\n",
    "\n",
    "θ=θ−(βXT(Xθ−Y)+αθ)\n",
    "\n",
    "其中β为步长。\n",
    "\n",
    "如果用最小二乘法，则θ的结果是：\n",
    "\n",
    "θ=(XTX+αE)−1XTY \n",
    "\n",
    "其中E为单位矩阵。\n",
    "\n",
    "Ridge回归在不抛弃任何一个变量的情况下，缩小了回归系数，使得模型相对而言比较的稳定，但这会使得模型的变量特别多，模型解释性差。\n",
    "\n",
    "Lasso则即又可以防止过拟合，同时克服Ridge回归模型变量多。\n",
    "Lasso回归有时也叫做线性回归的L1正则化，和Ridge回归的主要区别就是在正则化项，Ridge回归用的是L2正则化，而Lasso回归用的是L1正则化。Lasso回归的损失函数表达式如下：\n",
    "\n",
    "J(θ)=1/2n(Xθ−Y)T(Xθ−Y)+α||θ||1\n",
    "\n",
    "其中n为样本个数，α为常数系数，需要进行调优。||θ||1为L1范数。　　　\n",
    "\n",
    "Lasso回归使得一些系数变小，甚至还是一些绝对值较小的系数直接变为0，因此特别适用于参数数目缩减与参数的选择，因而用来估计稀疏参数的线性模型。但是Lasso回归有一个很大的问题，就是它的损失函数不是连续可导的，由于L1范数用的是绝对值之和，导致损失函数有不可导的点。也就是说，我们的最小二乘法，梯度下降法，牛顿法与拟牛顿法对它统统失效了。求有这个L1范数的损失函数极小值需要采用坐标轴下降法（coordinate descent）和最小角回归法（ Least Angle Regression， LARS）。\n",
    "Lasso回归是在ridge回归的基础上发展起来的，如果模型的特征非常多，需要压缩，那么Lasso回归是很好的选择。一般的情况下，普通的线性回归模型就够了。\n",
    "岭回归相对最小二乘是加了2范数正则下的最小二乘。当我们的feature之间完全共线性(使有解)或者相关程度异常高(使有稳定解)的时候，适合使用岭回归。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
